import { createSlice } from '@reduxjs/toolkit'
import Taro from '@tarojs/taro'

const initialState = {
  token: Taro.getStorageSync('token') || '',
  userInfo: JSON.parse(Taro.getStorageSync('userInfo') || '{}')
}

const userSlice = createSlice({
  name: 'user',
  initialState,
  reducers: {
    updateToken(state, { payload }) {
      state.token = payload
      Taro.setStorageSync('token', payload)
    },
    updateUserInfo(state, { payload }) {
      state.userInfo = payload
      Taro.setStorageSync('userInfo', JSON.stringify(payload))
    }
  }
})

export const { updateToken, updateUserInfo } = userSlice.actions
export default userSlice.reducer
